﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SyncApplication.Controls;
using AppCommon;
using SyncLib;
using SyncConfig.Controls;
using DevComponents.AdvTree;
using Microsoft.Synchronization.Data;
using Microsoft.Synchronization.Data.SqlServer;
using System.Data.SqlClient;


namespace SyncApplication
{
    public partial class FrmClient : Form
    {
        public static FrmClient Current { get; set; }
        public FrmClient()
        {
            InitializeComponent();
            Current = this;
            TreeManipulation = new ClientAdvTreeManipulation(advTree1);
            ctrlConfig = new ClientConfig();
            ctrlOptional = new Optional();
            ctrlClientHistorySummary = new ClientHistorySummary();
            ctrlChoiceScope4Sync = new ChoiceScope4Sync();
            if (CommonVariable.Get("ClientName") != null)
                Text = "Client-" + CommonVariable.Get("ClientName").ToString();
        }
        ClientConfig ctrlConfig = null;
        Optional ctrlOptional = null;
        ChoiceScope4Sync ctrlChoiceScope4Sync = null;
        public ClientAdvTreeManipulation TreeManipulation;
        ClientHistorySummary ctrlClientHistorySummary = null;
        private void FrmClient_Load(object sender, EventArgs e)
        {
            try
            {
                TreeManipulation.LoadData();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi kết nối tới máy chủ\n" + ex.Message, "Loi");
            }
            finally
            {
                TreeManipulation.BuildTree();
            }
        }

        private void advTree1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (advTree1.SelectedIndex < 0)
                {
                    panelEx2.Controls.Clear();
                    return;
                }
                if (advTree1.SelectedNode.Tag != null)
                {
                    if (advTree1.SelectedNode.Tag is SyncConfigDS.ScopeInfoExRow)
                    {
                        ctrlConfig.CurrentNode = advTree1.SelectedNode;
                        ctrlConfig.DataBind();
                        AddControl(ctrlConfig);
                    }
                }
                else
                {
                    panelEx2.Controls.Clear();

                    switch (advTree1.SelectedNode.Name)
                    {
                        case "Option":
                            ctrlOptional.LoadOption();
                            AddControl(ctrlOptional);
                            break;
                        case "Synchronize":
                            ctrlChoiceScope4Sync.DataBind();
                            AddControl(ctrlChoiceScope4Sync);
                            break;
                        case "History":
                            ctrlClientHistorySummary.DataBind();
                            AddControl(ctrlClientHistorySummary);
                            break;
                        case "ScopeRoot":
                            ctrlClientHistorySummary.DataBindSummary();
                            AddControl(ctrlClientHistorySummary);

                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        void AddControl(Control c)
        {
            c.Dock = DockStyle.Fill;
            c.BackColor = Color.Transparent;
            panelEx2.Controls.Clear();
            panelEx2.Controls.Add(c);
            c.SendToBack();
        }

        private void FrmClient_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;
                Hide();
                FrmNotify.Current.SyncAppNotify.ShowBalloonTip(1000, "Keeper Sync", "Chương trình vẫn chạy", ToolTipIcon.Info);
            }
        }

        private void bbiExit_Click(object sender, EventArgs e)
        {
            Application.ExitThread();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }

        public void SelectNode(string nodeName)
        {
            Node[] selecteds = advTree1.Nodes.Find(nodeName, false);
            if (selecteds.Length > 0)
                advTree1.SelectedNode = selecteds[0];
        }

        private void biReloadTree_Click(object sender, EventArgs e)
        {
            FrmClient_Load(sender, e);
        }

        private void buttonItem3_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection conn = new SqlConnection(CommonVariable.SqlConnectionString);
                SqlSyncScopeDeprovisioning deprovi = new SqlSyncScopeDeprovisioning(conn);
                deprovi.ObjectSchema = "Sync";
                deprovi.DeprovisionScope("ConfigScope");
            }
            catch (Exception ẽ)
            {
                MessageBox.Show(ẽ.Message);
            }
        }

    }
}
